import time

from 水质.utils.Logger import logger
from 水质.utils.click import clickText, clickId, swipeUp, swipeDown, wait_Id, wait_clickId
from appium.webdriver.common.appiumby import AppiumBy


def export_excel(driver):
    # 导出excel
    clickId(driver, "imageView19")
    wait_clickId(driver, "md_button_positive")
    wait_clickId(driver, "md_button_negative")


def clickFilter(driver, text):
    clickId(driver, "imageView13")
    clickText(driver, text)
    clickText(driver, "确认")


def checkDayMonth(driver):
    parent = driver.find_element(by=AppiumBy.ID, value="title_container")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 1:
        els[1].click()
        logger.log("点击月元素")

    else:
        logger.log("没有月元素切换")


def checkDay(driver):
    parent = driver.find_element(by=AppiumBy.ID, value="title_container")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 0:
        els[0].click()
        logger.log("点击日元素")

    else:
        logger.log("没有日元素切换")


def checkDatePick(driver):
    clickId(driver, "textView406")
    clickText(driver, "今天")
    clickText(driver, "确认")

    clickId(driver, "textView406")
    clickText(driver, "昨天")
    clickText(driver, "确认")
    clickId(driver, "textView406")
    parent = driver.find_element(by=AppiumBy.ID, value="datePickerView")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 0:
        year = els[0]
        swipeUp(driver, year)
    if len(els) > 1:
        year = els[1]
        swipeDown(driver, year)
    if len(els) > 2:
        year = els[2]
        swipeUp(driver, year)

    clickText(driver, "确认")


def checkTableFlux(driver):
    # clickId(driver, "iv_menu")
    # clickText(driver, "流量报表")
    #
    #
    # time.sleep(2)

    checkDayMonth(driver)
    checkDatePick(driver)
    checkMaxMinTimeExsit(driver)

    clickId(driver, "textView408")
    clickText(driver, "雷达水位计")
    checkMaxMinTimeExsit(driver)  ##随机在这个位置判断是不是空

    clickId(driver, "textView408")
    clickText(driver, "雷达流速仪")
    clickId(driver, "textView408")
    clickText(driver, "在线ADCP")
    clickId(driver, "textView407")
    clickText(driver, "现场流量数据")
    checkMaxMinTimeExsit(driver)

    clickId(driver, "textView407")
    clickText(driver, "平台率定后流量数据")
    # 综合筛选部分
    clickFilter(driver, "雷达水位计")
    clickFilter(driver, "雷达流速仪")
    checkMaxMinTimeExsit(driver)

    clickFilter(driver, "在线ADCP")
    clickFilter(driver, "现场流量数据")
    clickFilter(driver, "平台率定后流量数据")

    checkDay(driver)

    clickId(driver, "textView408")
    clickText(driver, "雷达水位计")
    clickId(driver, "textView408")
    clickText(driver, "雷达流速仪")
    checkMaxMinTimeExsit(driver)

    clickId(driver, "textView408")
    clickText(driver, "在线ADCP")
    clickId(driver, "textView407")
    clickText(driver, "现场流量数据")
    clickId(driver, "textView407")
    clickText(driver, "平台率定后流量数据")
    checkMaxMinTimeExsit(driver)

    # 综合筛选部分
    clickFilter(driver, "雷达水位计")
    clickFilter(driver, "雷达流速仪")
    clickFilter(driver, "在线ADCP")
    clickFilter(driver, "现场流量数据")
    clickFilter(driver, "平台率定后流量数据")

    export_excel(driver)


def checkMaxMinTimeExsit(driver):
    # 检查报表的最小最大时间是否存在，因为测试初期出现不存在情况
    tv_min_time_value = wait_Id(driver, "tv_min_time_value")
    # 获取 TextView 的文本内容
    text_content = tv_min_time_value.text
    # 判断文本内容是否为空
    if text_content.strip() == '':
        logger.error("tv_min_time_value 的文本内容为空")
    else:
        logger.log("tv_min_time_value 的文本内容不为空")
    tv_max_time_value = wait_Id(driver, "tv_max_time_value")
    # 获取 TextView 的文本内容
    text_content = tv_max_time_value.text
    # 判断文本内容是否为空
    if text_content.strip() == '':
        logger.error("tv_max_time_value 的文本内容为空")
    else:
        logger.log("tv_max_time_value 的文本内容不为空")
